<?php
/**
 * Selectors (Requiere Helper: string_my_helper)
 * 
 * deepsteal@hotmail.com
 */
// ------------------------------------------------------------------------

/**
 * 
 * Add_class
 *
 * Descripcion
 * 
 * @author Edgardo Segura 
 * @example
 * @see
 * @link {}
 * @since 16/02/2009
 * @version
 * @access	public
 * @param	string
 * @return	string
 * 
 */	
if ( ! function_exists('add_class'))
{
	function add_class($class_name,$selector,$in){
		
		$content_selector=selector($selector,$in);
		
		foreach ($content_selector as $actual){
			if(substr_hexist($actual,'class="')){
				$content_selector_m=str_replace('class="','class="'.$class_name.' ',$actual);
				$in = str_replace($actual,$content_selector_m,$in);
			}else{
				$space=strpos($actual,' ');
				$content_selector_m=substr($actual,0,$space).' class="'.$class_name.'" '.substr($actual,$space+1);
				$in = str_replace($actual,$content_selector_m,$in);
			}		
		}	
		return $in;
	}
}
// ------------------------------------------------------------------------

/**
 * 
 * Get_html
 *
 * Descripcion
 * 
 * @author Edgardo Segura 
 * @example
 * @see
 * @link {}
 * @since 16/02/2009
 * @version
 * @access	public
 * @param	string
 * @return	string
 * 
 */
if ( ! function_exists('get_html'))
{
	function get_html($selector,$in){
		$content_selector=selector($selector,$in);
		
		return implode('',$content_selector);
	}
}
// ------------------------------------------------------------------------

/**
 * 
 * Get_attrib_val
 *
 * Descripcion
 * 
 * @author Edgardo Segura 
 * @example
 * @see
 * @link {}
 * @since 16/02/2009
 * @version
 * @access	public
 * @param	string
 * @return	string
 * 
 */
if ( ! function_exists('get_attrib_val'))
{
	function get_attrib_val($attrib_name,$content){
		return since_until($content,$attrib_name.'="','"');
	}
}
// ------------------------------------------------------------------------

/**
 * 
 * Set_attrib_val
 *
 * Descripcion
 * 
 * @author Edgardo Segura 
 * @example
 * @see
 * @link {}
 * @since 16/02/2009
 * @version
 * @access	public
 * @param	string
 * @return	string
 * 
 */
if ( ! function_exists('set_attrib_val'))
{
	function set_attrib_val($attrib_name,$content,$val){
		
	}
}
// ------------------------------------------------------------------------

/**
 * 
 * Add_attrib_val
 *
 * Descripcion
 * 
 * @author Edgardo Segura 
 * @example
 * @see
 * @link {}
 * @since 16/02/2009
 * @version
 * @access	public
 * @param	string
 * @return	string
 * 
 */
if ( ! function_exists('add_attrib_val'))
{
	function add_attrib_val($attrib_name,$content,$val){
		$val=get_attrib_val($attrib_name,$content).' '.$add_val;
		
	}
}
// ------------------------------------------------------------------------

/**
 * Value_inject
 * 
 * Retorna una cadena de texto con otra insertada en DESDE HASTA dados del tipo attrib value en html ej.: class="inject_aqui"
 *
 * @access	public
 * @param	string , array
 * @return	string
 */
if ( ! function_exists('value_inject'))
{
	function value_inject($contenido,$en,$attrib_name){
		
		$desde=$attrib_name.'="';
		$hasta='"';
		
		$aparicion1 = strpos($en,$desde)-1;
		$aparicion2 = substr($en,$aparicion1+strlen($desde)+1);
		$aparicion2 = $aparicion1+strlen($desde)+strpos($aparicion2,$hasta)+1;
		
		$principio = substr($en,0,$aparicion1+strlen($desde)+1);
		
		$final = substr($en,$aparicion2);
		
		return $principio.$contenido.$final;	
	}
}
// ------------------------------------------------------------------------

/**
 * 
 * Selector
 *
 * Descripcion
 * 
 * @author Edgardo Segura 
 * @example
 * @see
 * @link {}
 * @since 16/02/2009
 * @version
 * @access	public
 * @param	string
 * @return	string
 * 
 */
if ( ! function_exists('selector'))
{
	function selector($string_selectors,$string_base){
		$array_selectors=explode(' ',$string_selectors);
		
		foreach ($array_selectors as $actual){
			switch ($actual) {
				case $actual[0]=='.':
					if(is_array($result))
					{
						$result[] = detect_by_class($actual,$result[count($result)-1]);
					}else{
						$result[] = detect_by_class($actual,$string_base);
					}
				break;
				
				case $actual[0]=='#':
					if(is_array($result))
					{
						$result[] = detect_by_id($actual,$result[count($result)-1]);
					}else{
						$result[] = detect_by_id($actual,$string_base);
					}
				break;
				
				case substr_exist($actual,'content:'):
					if(is_array($result))
					{
						$result[] = detect_by_content($actual,$result[count($result)-1]);
					}else{
						$result[] = detect_by_content($actual,$string_base);
					}
				break;
				
				default:
				if(is_array($result))
					{
						$result[] = detect_by_tag_name($actual,$result[count($result)-1]);
					}else{
						$result[] = detect_by_tag_name($actual,$string_base);
					}
				break;
				}
		}
		
		return force_array($result[count($result)-1]);
	}
}
// ------------------------------------------------------------------------

/**
 * 
 * Detect_by_class
 *
 * Descripcion
 * 
 * @author Edgardo Segura 
 * @example
 * @see
 * @link {}
 * @since 16/02/2009
 * @version
 * @access	public
 * @param	string
 * @return	string
 * 
 */
if ( ! function_exists('detect_by_class'))
{
	function detect_by_class($name,$in){
		$name = str_replace('.','',$name);
		
		$result= tag_around_name($name,$in,'class');
		
		return since_until($in,$result['into'],close_tag($result['name']));
	}
}
// ------------------------------------------------------------------------

/**
 * 
 * Detect_by_id
 *
 * Descripcion
 * 
 * @author Edgardo Segura 
 * @example
 * @see
 * @link {}
 * @since 16/02/2009
 * @version
 * @access	public
 * @param	string
 * @return	string
 * 
 */
if ( ! function_exists('detect_by_id'))
{
	function detect_by_id($name,$in){
		$name = str_replace('#','',$name);
		
		$result= tag_around_name($name,$in,'id');
		
		return since_until($in,$result['into'],close_tag($result['name']));
	}
}
// ------------------------------------------------------------------------

/**
 * 
 * Detect_by_content
 *
 * Descripcion
 * 
 * @author Edgardo Segura 
 * @example
 * @see
 * @link {}
 * @since 16/02/2009
 * @version
 * @access	public
 * @param	string
 * @return	string
 * 
 */
if ( ! function_exists('detect_by_content'))
{
	function detect_by_content($name,$in){
		$content_name=str_replace('content:','',$name);
		
		$in_r=force_array($in);
		
		foreach ($in_r as $actual){
			$content = until(since($actual,'>'),'<');
			if(substr_hexist($content,$content_name)){
				$result[] = $actual;
			}
		}	
		return $result;
	}
}
// ------------------------------------------------------------------------

/**
 * 
 * Detect_by_tag_name
 *
 * Descripcion
 * 
 * @author Edgardo Segura 
 * @example
 * @see
 * @link {}
 * @since 16/02/2009
 * @version
 * @access	public
 * @param	string
 * @return	string
 * 
 */
if ( ! function_exists('detect_by_tag_name'))
{
	function detect_by_tag_name($name,$in){
		$items=explode('<'.$name,trim($in));
		foreach ($items as $actual){
			if(strlen($actual)>0){
				$result[]='<'.$name.since_until($in,'<'.$name,close_tag($name)).close_tag($name);
				$in = str_replace($result[count($result)-1],'',$in);
			}			
		}
		return $result;
	}
}
// ------------------------------------------------------------------------

/**
 * 
 * Close_tag
 *
 * Descripcion
 * 
 * @author Edgardo Segura 
 * @example
 * @see
 * @link {}
 * @since 16/02/2009
 * @version
 * @access	public
 * @param	string
 * @return	string
 * 
 */
if ( ! function_exists('close_tag'))
{	
	function close_tag($tag_name){
		return '</'.$tag_name.'>';
	}
}
// ------------------------------------------------------------------------

/**
 * 
 * Tag_around_name
 *
 * Descripcion
 * 
 * @author Edgardo Segura 
 * @example
 * @see
 * @link {}
 * @since 16/02/2009
 * @version
 * @access	public
 * @param	string
 * @return	string
 * 
 */
if ( ! function_exists('tag_around_name'))
{
	function tag_around_name($name,$in,$tag_name){
		$name = str_replace(array('.','#'),'',$name);
		$long = strlen($in);
		$start = strpos($in,$name);
		
		for($i=$start;$i>-1;$i--)
		{
			if($in[$i]=='<'){
				$tag_ini=$i;
				break;
			}
		}
		
		for($i=$start;$i<$long;$i++)
		{
			if($in[$i]=='>'){
				$tag_fin=$i+1;
				break;
			}
		}
		
		$tag_parcial=substr($in,0,$tag_fin);
		$tag['into']=substr($tag_parcial,$tag_ini);
		$tag['name']=substr(until($tag['into'],' '),1);
		$tag['value']=since_until($tag['into'],$tag_name.'="','"');
		$tag['ini']=$tag_ini;
		$tag['fin']=$tag_fin;
		
		return $tag;
	}
}
// ------------------------------------------------------------------------

/* End of file selectors_my_helper.php */
/* Location: ./system/application/helpers/selectors_my_helper.php */